package com.cloudfun.campusshare.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.bean.copier.CopyOptions;
import com.cloudfun.campusshare.common.model.dto.DeviceCsqDTO;
import com.cloudfun.campusshare.common.model.vo.DeviceCsqListVO;
import com.cloudfun.campusshare.common.response.PageResult;
import com.cloudfun.campusshare.entity.DeviceCsqEntity;
import com.cloudfun.campusshare.repo.DeviceCsqRepo;
import com.cloudfun.campusshare.service.DeviceCsqService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;

import javax.persistence.criteria.Predicate;
import java.time.ZoneOffset;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * Created by CodeGenerator on 2020/09/27
 */
@Slf4j
@Service
public class DeviceCsqServiceImpl implements DeviceCsqService {

    @Autowired
    private DeviceCsqRepo deviceCsqRepo;

    @Override
    public void create(DeviceCsqDTO deviceCsqDTO) {
        DeviceCsqEntity deviceCsqEntity = new DeviceCsqEntity();
        BeanUtils.copyProperties(deviceCsqDTO, deviceCsqEntity);
        deviceCsqRepo.save(deviceCsqEntity);
    }

    @Override
    public DeviceCsqEntity getLatestByImei(String imei) {
        return deviceCsqRepo.findTopByImeiOrderByCreateTimeDesc(imei);
    }

    @Override
    public PageResult<DeviceCsqListVO> queryByImei(String imei, Pageable pageable) {
        pageable = PageRequest.of(pageable.getPageNumber(), pageable.getPageSize(), Sort.by(Sort.Direction.DESC, "createTime"));
        Specification<DeviceCsqEntity> specification = (root, query, criteriaBuilder) -> {
            List<Predicate> predicateList = new ArrayList<>();
            if (StringUtils.isNotBlank(imei)) {
                predicateList.add(criteriaBuilder.equal(root.get("imei"), imei));
            }
            Predicate[] predicates = new Predicate[predicateList.size()];
            predicateList.toArray(predicates);
            return criteriaBuilder.and(predicates);
        };
        Page<DeviceCsqEntity> entityPage = deviceCsqRepo.findAll(specification, pageable);
        long totalElements = entityPage.getTotalElements();
        List<DeviceCsqListVO> content = entityPage.getContent().stream()
                .map(appEntity -> {
                    DeviceCsqListVO vo = new DeviceCsqListVO();
                    BeanUtil.copyProperties(appEntity, vo, CopyOptions.create().setIgnoreProperties("createTime"));
                    vo.setCreateTime("" + appEntity.getCreateTime().toInstant(ZoneOffset.ofHours(8)).toEpochMilli());
                    return vo;
                }).collect(Collectors.toList());
        PageResult<DeviceCsqListVO> result = new PageResult<>();
        result.setElements(content);
        result.setTotalSize(totalElements);
        return result;
    }
}